package com.yang.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yang.dto.req.OrderDetailPageQueryReqDTO;
import com.yang.dto.req.OrderDetailSaveReqDTO;
import com.yang.dto.req.OrderDetailUpdateReqDTO;
import com.yang.dto.resp.OrderDetailPageQueryRespDTO;
import com.yang.result.Result;
import com.yang.service.OrderDetailService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * 订单详情控制层
 * 作者：孤城暖阳y
 * 日期：2025-03-02
 */
@RestController
@RequiredArgsConstructor
@Tag(name = "订单详情管理")
public class OrderDetailController {

    private final OrderDetailService orderDetailService;

    @Operation(summary = "分页查询订单详情")
    @GetMapping("/Treasure-Pavilion-admin/orderDetail/page")
    public Result<IPage<OrderDetailPageQueryRespDTO>> findAll(OrderDetailPageQueryReqDTO requestParam) {
        return Result.success(orderDetailService.pageQueryOrderDetail(requestParam));
    }

    @Operation(summary = "查询未发货订单数")
    @GetMapping("/Treasure-Pavilion-admin/orderDetail/countUndelivered")
    public Result<Long> countUndelivered() {
        return Result.success(orderDetailService.getOrderCountUndeliveredCount());
    }

    @Operation(summary = "今日销量")
    @GetMapping("/Treasure-Pavilion-admin/orderDetail/todaySales")
    public Result<Long> todaySales() {
        return Result.success(orderDetailService.getTodaySales());
    }




    @Operation(summary = "更新订单详情信息")
    @PostMapping("/Treasure-Pavilion-admin/orderDetail/update")
    @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "更新订单详情信息", required = true)
    public Result updateOrderDetail(@Valid @RequestBody OrderDetailUpdateReqDTO requestParam) {
        // 验证订单详情ID是否存在，姓名、手机号、地址描述是否为空
        if (Objects.isNull(requestParam.getReceiverName()) &&
                Objects.isNull(requestParam.getReceiverPhone()) &&
                Objects.isNull(requestParam.getReceiverAddressDescription()) &&
                Objects.isNull(requestParam.getStatus())) {
            throw new RuntimeException("更新信息不能为空");
        }
        orderDetailService.updateOrderDetail(requestParam);
        return Result.success();
    }

    @Operation(summary = "增加订单详情信息")
    @PostMapping("/Treasure-Pavilion-admin/orderDetail/save")
    public Result saveOrderDetail(@Valid OrderDetailSaveReqDTO requestParam) {
        return Result.success();
    }

//    TODO: 订单详情一般不会删除，所以注释掉 需要再考虑
//    @Operation(description = "删除订单详情信息")
//    @PostMapping("/Treasure-Pavilion-admin/orderDetail/delete")
//    public Result<Boolean> deleteOrderDetail(@Valid OrderDetailDeleteReqDTO requestParam) {
//        return Result.success();
//    }

}
